﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CoreNhaDat
{
    public class GiaBll
    {
        public static int Insert(nlit_Gia Info, string strConnect)
        {
            int id = 0;
            using (NhaDatToLinQDataContext db = new NhaDatToLinQDataContext(strConnect))
            {
                try
                {
                    db.nlit_Gias.InsertOnSubmit(Info);
                    db.SubmitChanges();
                    id = Info.GiaId;
                }
                catch { id = 0; }
                finally { db.Dispose(); }
            }
            return id;
        }

        public static bool Update(nlit_Gia Info, string strConnect)
        {
            bool res = false;
            using (NhaDatToLinQDataContext db = new NhaDatToLinQDataContext(strConnect))
            {
                try
                {
                    nlit_Gia o = db.nlit_Gias.Single(p => p.GiaId == Info.GiaId);
                    if (o != null)
                    {
                        o.Name = Info.Name;
                        db.SubmitChanges();
                        res = true;
                    }
                }
                catch { }
                finally { db.Dispose(); }
            }
            return res;
        }

        public static bool UpdateOrderDisplay(int id, int orderDisplay, string strConnect)
        {
            bool res = false;
            using (NhaDatToLinQDataContext db = new NhaDatToLinQDataContext(strConnect))
            {
                try
                {
                    nlit_Gia o = db.nlit_Gias.Single(p => p.GiaId == id);
                    if (o != null)
                    {
                        o.OrderDisplay = orderDisplay;
                        db.SubmitChanges();
                        res = true;
                    }
                }
                catch { }
                finally { db.Dispose(); }
            }
            return res;
        }

        public static bool UpdateIsDeleted(int id, bool isDeleted, string strConnect)
        {
            bool res = false;
            using (NhaDatToLinQDataContext db = new NhaDatToLinQDataContext(strConnect))
            {
                try
                {
                    nlit_Gia o = db.nlit_Gias.Single(p => p.GiaId == id);
                    if (o != null)
                    {
                        o.IsDeleted = isDeleted;
                        db.SubmitChanges();
                        res = true;
                    }
                }
                catch { }
                finally { db.Dispose(); }
            }
            return res;
        }

        public static bool UpdateIsActive(int id, bool isActive, string strConnect)
        {
            bool res = false;
            using (NhaDatToLinQDataContext db = new NhaDatToLinQDataContext(strConnect))
            {
                try
                {
                    nlit_Gia o = db.nlit_Gias.Single(p => p.GiaId == id);
                    if (o != null)
                    {
                        o.IsActive = isActive;
                        db.SubmitChanges();
                        res = true;
                    }
                }
                catch { }
                finally { db.Dispose(); }
            }
            return res;
        }

        public static List<nlit_Gia> GetList(bool isDeleted, bool isActive, string strConnect)
        {
            List<nlit_Gia> List = new List<nlit_Gia>();

            using (NhaDatToLinQDataContext db = new NhaDatToLinQDataContext(strConnect))
            {
                try
                {
                    var varlist = from p in db.nlit_Gias
                                  orderby p.OrderDisplay
                                  where p.IsDeleted == isDeleted && p.IsActive == isActive
                                  select p;
                    List = varlist.ToList();
                }
                catch
                {
                    List = null;
                }
                finally
                {
                    db.Dispose();
                }
            }
            return List;
        }

        public static List<nlit_Gia> GetList(string strConnect)
        {
            List<nlit_Gia> List = new List<nlit_Gia>();

            using (NhaDatToLinQDataContext db = new NhaDatToLinQDataContext(strConnect))
            {
                try
                {
                    var varlist = from p in db.nlit_Gias
                                  where p.IsDeleted == false
                                  orderby p.OrderDisplay
                                  select p;
                    List = varlist.ToList();
                }
                catch
                {
                    List = null;
                }
                finally
                {
                    db.Dispose();
                }
            }
            return List;
        }

        public static nlit_Gia GetById(int id, string strConnect)
        {
            nlit_Gia res = new nlit_Gia();

            using (NhaDatToLinQDataContext db = new NhaDatToLinQDataContext(strConnect))
            {
                try
                {

                    res = db.nlit_Gias.Single(p => p.GiaId == id);

                }
                catch
                {

                }
                finally
                {
                    db.Dispose();
                }
            }
            return res;
        }
    }
}
